# -*- coding:utf-8 -*-

import requests
import json
import time
from datetime import datetime, timedelta
import csv

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
微博评论
https://m.weibo.cn/comments/hotflow?id=4451582411926548&mid=4451582411926548&max_id_type=0
"""

class WeiBo_Get():
    def __init__(self, file_name, cookies):
        self.headers = {
            "cookie": cookies,
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36",
        }
        self.file_name = file_name
        with open("./" + self.file_name + ".csv", "a", encoding="utf-8-sig", newline="") as csvfile:
            csv_writer = csv.writer(csvfile)
            csv_writer.writerow(["评论ID", "时间", "评论内容", "用户名称"])

    # 获取评论内容
    def get_comments(self, id, max_id, max_id_type):
        postData = {
            "id": id,
            "mid": id,
            "max_id_type": "0",
        }

        if max_id != "0":
            postData['max_id'] = str(max_id)
            postData['max_id_type'] = str(max_id_type)
        url = "https://m.weibo.cn/comments/hotflow"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)
        num = 0  # 计算已获取评论数量
        if data_json['ok'] == 1:
            count = data_json['data']['total_number']  # 总数量
            max_id = data_json['data']['max_id']  # 下页游标
            max_id_type = data_json['data']['max_id_type']  #
            for d in data_json['data']['data']:
                num += 1
                dict_data = dict()
                dict_data['id'] = d['id'] + "\t"  # 评论ID
                # 评论时间
                timeStruct = time.strptime(d['created_at'], '%a %b %d %X %z %Y')
                dict_data['created_at'] = time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
                dict_data['text'] = d['text']  # 评论内容
                dict_data['user_name'] = d['user']['screen_name']  # 用户名称
                print(f"--------已获取评论数量：{num}-------------")
                print(str(dict_data))

                with open("./" + self.file_name + ".csv", "a", encoding="utf-8-sig", newline="") as csvfile:
                    fileheader = ["id", "created_at", "text",  "user_name"]
                    writer = csv.DictWriter(csvfile, fieldnames=fileheader)
                    writer.writerow(dict_data)
                # 获取回复内容
                if d['total_number'] > 0:
                    self.get_reply_comments(d['id'], 0, 0, 0)
            time.sleep(1)
            self.get_comments(id, max_id, max_id_type)
        else:
            print("==========获取评论结束==================")


    # 获取评论回复内容
    def get_reply_comments(self, cid, max_id, max_id_type, count):
        postData = {
            "cid": cid,
            "max_id": "0",
            "max_id_type": "0",
        }

        if max_id != "0":
            postData['max_id'] = str(max_id)
            postData['max_id_type'] = str(max_id_type)
        url = "https://m.weibo.cn/comments/hotFlowChild"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)
        if data_json['ok'] == 1 and len(data_json['data']) > 0:
            total_count = data_json['total_number']  # 总数量
            max_id = data_json['max_id']  # 下页游标
            max_id_type = data_json['max_id_type']  #
            for d in data_json['data']:
                count += 1
                dict_data = dict()
                dict_data['id'] = d['id'] + "\t"  # 评论ID
                # 评论时间
                timeStruct = time.strptime(d['created_at'], '%a %b %d %X %z %Y')
                dict_data['created_at'] = time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
                dict_data['text'] = d['text']  # 评论内容
                dict_data['user_name'] = d['user']['screen_name']  # 用户名称
                print(f"--------已获取回复数量：{count}-------------")
                print(str(dict_data))

                with open("./" + self.file_name + ".csv", "a", encoding="utf-8-sig", newline="") as csvfile:
                    fileheader = ["id", "created_at", "text", "user_name"]
                    writer = csv.DictWriter(csvfile, fieldnames=fileheader)
                    writer.writerow(dict_data)

                # 限定获取评论数量
                if count >= total_count:
                    return
            # if total_count > count:
            time.sleep(1)
            self.get_reply_comments(cid, max_id, max_id_type, count)
        else:
            print("==========获取评论结束==================")



if __name__ == '__main__':
    # 保存文件名
    fileName = "微博评论"
    # cookies
    cookies = ""
    # 微博评id
    id = "4528046339526327"
    weibo_get = WeiBo_Get(fileName, cookies)

    weibo_get.get_comments(id, "0", "0")

    # # 获取回复内容
    # weibo_get.get_reply_comments("4528047065670664", "0", "0", 0)